package com.lianmai.chaincard.view;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;

import com.chaychan.library.BottomBarItem;
import com.chaychan.library.BottomBarLayout;
import com.lianmai.chaincard.R;
import com.lianmai.chaincard.view.fragment.BasePagerFragmentAdapter;
import com.lianmai.chaincard.view.fragment.HomeMineFragment;
import com.lianmai.chaincard.view.fragment.HomePopularizeFragment;
import com.lianmai.chaincard.view.fragment.HomeShopFragment;
import com.lianmai.chaincard.view.fragment.HomeTradePulseFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 创建日期：2018/8/15 0015
 * 作者:Kite
 * 主界面
 */
public class HomePageActivity extends BaseActivity {

    @BindView(R.id.vp_layout)
    ViewPager vpLayout;

    @BindView(R.id.bbi_popularize)
    BottomBarItem bbiPopularize;

    @BindView(R.id.bbi_trade_pulse)
    BottomBarItem bbiTradePulse;

    @BindView(R.id.iv_add)
    ImageView ivAdd;

    @BindView(R.id.bbi_shop)
    BottomBarItem bbiShop;

    @BindView(R.id.bbi_mine)
    BottomBarItem bbiMine;

    @BindView(R.id.bbl)
    BottomBarLayout bbl;

    private List<Fragment> fragmentList = new ArrayList<>();

    @Override
    public int getLayoutId() {
        return R.layout.activity_home_page;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
    }

    @Override
    public void initView() {
        super.initView();
        initListener();
        initFragment();
    }

    private void initFragment() {
        fragmentList.add(HomePopularizeFragment.newInstance());
        fragmentList.add(HomeTradePulseFragment.newInstance());
        fragmentList.add(HomeShopFragment.newInstance());
        fragmentList.add(HomeMineFragment.newInstance());
        BasePagerFragmentAdapter fragmentAdapter = new BasePagerFragmentAdapter(getSupportFragmentManager(), fragmentList);
        vpLayout.setAdapter(fragmentAdapter);
        vpLayout.setOffscreenPageLimit(4);
        vpLayout.setCurrentItem(0);
    }

    private void initListener() {
//                mVpContent.setAdapter(new MyAdapter(getSupportFragmentManager()));
//        bbl.setViewPager(mVpContent);
        bbl.setOnItemSelectedListener(new BottomBarLayout.OnItemSelectedListener() {
            @Override
            public void onItemSelected(final BottomBarItem bottomBarItem, int previousPosition, final int currentPosition) {
                if (currentPosition < 2) {
                    vpLayout.setCurrentItem(currentPosition);
                    BottomBarItem bottomItem = bbl.getBottomItem(currentPosition);
                } else if (currentPosition > 2) {
                    vpLayout.setCurrentItem(currentPosition - 1);
                    BottomBarItem bottomItem = bbl.getBottomItem(currentPosition);
                }
            }
        });
        bbl.setUnread(0, 20);//设置第一个页签的未读数为20
        bbl.setUnread(1, 99);//设置第二个页签的未读数
        bbl.showNotify(3);//设置第三个页签显示提示的小红点
        bbl.setMsg(4, "NEW");//设置第四个页签显示NEW提示文字
        ivAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                advance(WebViewActivity.class,"http://www.qidian.com");
            }
        });
    }

}
